export default {
	state: {
		list: [{
				checked: false, //是否选中
				id: 11,
				title: "商品标题111",
				cover: "/static/images/demo/list/1.jpg",
				pprice: 200,
				num: 1, //当前的值
				minnum: 1,
				maxnum: 10, // 该商品最大商品数，跟库存有关
			},
			{
				checked: false, //是否选中
				id: 12,
				title: "商品标题222",
				cover: "/static/images/demo/list/2.jpg",
				pprice: 100,
				num: 2,
				minnum: 1,
				maxnum: 10, // 该商品最大商品数，跟库存有关
			},
			{
				checked: false, //是否选中
				id: 13,
				title: "商品标题333",
				cover: "/static/images/demo/list/3.jpg",
				pprice: 300,
				num: 1,
				minnum: 1,
				maxnum: 10, // 该商品最大商品数，跟库存有关
		},
		],
		selectedAll: [] //放置选中id
	},
	getters: {
		//全选
		checkedAll(state) {
			return state.list.length == state.selectedAll.length
		},
		//计算总价
		totalPrice(state) {
			let total = 0
			state.list.map(item => {
				if (item.checked) {
					total += item.pprice * item.num
				}
			})
			return total
		},
		isListShow(state){
			return state.list.length ==0
		}
	},
	mutations: {
		//初始化购物车
		initCartList(state,payload){
			state.list = payload
			
		},
		//退出清空购物车
		clearCartList(state){
			state.list = [];
			state.selectedAll=[]
			
		},
		//单选
		selectItem(state, index) {
			let id = state.list[index].id
			let i = state.selectedAll.indexOf(id)
			//当前我们是找到选中的
			if (i > -1) {
				//选中的checked变成未选中
				state.list[index].checked = false
				//state.selectedAll里面的id去掉
				state.selectedAll.splice(i, 1)
			} else { //当前我们是没有选中
				state.list[index].checked = true
				state.selectedAll.push(id)
			}
		},
		//全选
		selectAll(state) {
			state.selectedAll = state.list.map(item => {
				item.checked = true;
				return item.id
			})
			// state.list.map(item => {这个写法错误
			// 	item.checked = true;
			// 	state.selectedAll.push(item.id)
			// 	console.log('state.selectedAll',state.selectedAll);
			// })
		},
		//全不选
		unSelectAll(state) {
			state.list.map(item => {
				item.checked = false
			})
			state.selectedAll = []
		},
		//删除
		delSelect(state){
			  state.list = state.list.map(item => {
			        if (!item.checked) {
			            return item;
			        } else {
			            return null;
			        }
			    }).filter(item => item !== null);
			    state.selectedAll = false;
			// state.list = state.list.filter(item=>{
			// 	console.log('del',item.checked);
			// 	return !item.checked
			// })
			// state.selectedAll = []
		}
		

	},
	actions: {
		getcartlist(context){
			
		},
		doSelectAll(context) {
			context.getters.checkedAll ? context.commit('unSelectAll') : context.commit('selectAll')
		},
		doDelSelect(context){
			uni.showModal({
				title: '提示',
				content: '您确定要删除吗',
				success: (res)=> {
					if (res.confirm) {
						console.log('用户点击确定');
						context.commit('delSelect')
						
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});

		}
	}
}